package com.tanhua.listener;

import com.tanhua.domain.PublishScore;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
@RocketMQMessageListener(
    topic = "tanhua-bigdata",
    consumerGroup="tanhua-bigdata-big"
)
public class PublishScoreListener implements RocketMQListener<Map> {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void onMessage(Map map) {
        //用户id
        long userId = Long.parseLong(map.get("userId").toString());
        //大数据id
        long pid = Long.parseLong(map.get("pid").toString());
        //操作的类型
        int type = Integer.parseInt(map.get("type").toString());
        //封装数据到实体 保存publish_score表中
        PublishScore publishScore = new PublishScore();
        publishScore.setUserId(userId);
        publishScore.setPublishId(pid);
        publishScore.setDate(new Date().getTime());

        //type是操作行为 转成行为对应的分数
        Double score = 0D;
        /*- 操作1： 浏览 +1
        - 操作2： 点赞 +5
        - 操作3： 喜欢 +8
        - 操作4： 评论 + 10
        - 操作5：取消点赞 -5
        - 操作6：取消喜欢 -8*/
        switch (type){
            case 1:{
                score = 1D;
                break;
            }case 2:{
                score = 5D;
                break;
            }case 3:{
                score = 8D;
                break;
            }case 4:{
                score = 10D;
                break;
            }case 5:{
                score = -5D;
                break;
            }case 6:{
                score = -8D;
                break;
            }default:{
                break;
            }
        }
        publishScore.setScore(score);


        //保存到对应的数据库中
        mongoTemplate.insert(publishScore);

        System.out.println("此时publish_score表中就有大数据需要的内容了...");

    }

}
